{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "提取用于建立预测模型的基础数据，并对其进行单位根检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "指标  High  单位根检验的p值为： 0.9955202283491458\n",
      "指标  Low  单位根检验的p值为： 0.9942509444701155\n",
      "指标  Open  单位根检验的p值为： 0.9938548220926627\n",
      "指标  Close  单位根检验的p值为： 0.9950049120865715\n"
     ]
    }
   ],
   "source": [
    "import statsmodels.tsa.stattools as stat\n",
    "import pandas_datareader.data as web\n",
    "import datetime as dt\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pickle\n",
    "\n",
    "data = web.DataReader('600519.ss','yahoo', dt.datetime(2014,1,1),dt.datetime(2019,9,30))\n",
    "with open('data/data.pkl', 'wb') as f:\n",
    "    pickle.dump(data, f)\n",
    "subdata = data.iloc[:-30,:4]\n",
    "for i in range(4):\n",
    "    pvalue = stat.adfuller(subdata.values[:,i], 1)[1]\n",
    "    print(\"指标 \",data.columns[i],\" 单位根检验的p值为：\",pvalue)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现对subdata进行一阶差分运算，并再次进行进行单位根检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "指标  High  单位根检验的p值为： 0.0\n",
      "指标  Low  单位根检验的p值为： 0.0\n",
      "指标  Open  单位根检验的p值为： 0.0\n",
      "指标  Close  单位根检验的p值为： 0.0\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "subdata_diff1 = subdata.iloc[1:,:].values - subdata.iloc[:-1,:].values\n",
    "\n",
    "with open('data/subdata_diff1.pkl', 'wb') as f:\n",
    "    pickle.dump(subdata_diff1, f)\n",
    "    \n",
    "for i in range(4):\n",
    "    pvalue = stat.adfuller(subdata_diff1[:,i], 1)[1]\n",
    "    print(\"指标 \",data.columns[i],\" 单位根检验的p值为：\",pvalue)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
